package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/vehicle/")
@Tag(name = "车辆管理模块")
@Validated
public class VehicleController {

    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "01-添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session) {
        log.debug("开始处理【添加车辆】的请求..."+vehicleAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");

            Vehicle vehicle = new Vehicle();
            BeanUtils.copyProperties(vehicleAddParam,vehicle);
            vehicle.setCreateTime(new Date());
            vehicle.setUpdateTime(new Date());
            vehicle.setUserId(userVO.getId());
            int rows = vehicleMapper.insertVehicle(vehicle);
            if (rows > 0){
                return JsonResult.ok();
            }
            return new JsonResult(StatusCode.UKNOWN_ERROR);

    }
    @GetMapping("list")
    @Operation(summary = "02-查询车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand"),
            @Parameter(name = "license"),
            @Parameter(name = "vehicleListQuery", hidden = true),
    })
    public JsonResult list(VehicleListQuery vehicleListQuery , HttpSession session) {
        log.debug("开始处理【查询车辆列表】的请求，参数：{}", vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");

            List<Vehicle> list = vehicleMapper.selectlist(userVO.getId(),vehicleListQuery);
            return JsonResult.ok(list);


    }
    @PostMapping("del/{id}")
    @Operation(summary = "03-删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteVehilce(@PathVariable @Range(min = 1L,max = 500L ,
            message = "车辆id范围1-500") Long id, HttpSession session) {
        //手动抛出异常，测试全局异常处理
        if (id < 0){
            throw new IllegalArgumentException("车辆id不能小于0");
        }
        log.debug("开始处理【删除车辆】的请求，参数：{}", id);
        UserVO userVO = (UserVO) session.getAttribute("user");

            int rows = vehicleMapper.updateIsDelete(id, userVO.getId());
            if (rows > 0) {
                return JsonResult.ok();
            } else {
                return new JsonResult(StatusCode.OPERATION_FAILED);
            }

    }
}
